/* b_nsc_college.do -***********************************************************

	Conducts analysis using NSC outcomes. Estimates outputted here are used in
	Table 3 and appendix Tables A6 through A10.

******************************************************************************/
set more off

local specs "an_fr_K1 an_fr_K1_cntrl an_fr_K1_fem"
use "$stata_data_analysis/final_sample.dta", clear

* Pscore interactions (used in Table A10)
	* Demean p-score for interpretability
	su pformany
	gen pformany_demean = pformany - r(mean)

	* Generate new variable that interact endogenous with p-score
	gen d_psc_int = att * pformany_demean

	* Generate new variable that interact instrument with p-score
	gen z_psc_int = soffany * pformany_demean

forval i = 1/7 {
	display in red "loop `i'"

	local outcomes_set any_6m_asgn any2yr_nsc_6m_asgn any4yr_nsc_6m_asgn instate_nsc_6m_asgn ///
					public_nsc_6m_asgn private_nsc_6m_asgn everany_nsc everany2yr_nsc ///
					everany4yr_nsc everinstate_nsc everpublic_nsc everprivate_nsc ///
					num_sem evergrad_nsc evergrad_nsc_4yr grad_ontime_asgn grad_4yr_ontime_asgn_4yr

*This is the analysis that conducts analysis of NSC outcomes with no controls,
*which is used in the subgroup analysis at the end of this .do file
if `i' == 1 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn"
	local pscore			"pformany"
	local outcomes			`outcomes_set'
	local restriction		"in_K1"
	local suffix			"an_fr_K1"
	local demographic_list 	"hisp asian black white fem male not_frpl frpl inc1 inc2 inc3 inc4"
}

*This is the analysis that basic NSC analysis with demographic controls (Table 3)
if `i' == 2 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn hispanic female black white sp_biling"
	local pscore			"pformany"
	local outcomes			`outcomes_set'
	local restriction		"in_K1"
	local suffix			"an_fr_K1_cntrl"
	local demographic_list	""
}

*This is the analysis that basic NSC analysis with demographic controls, but with linear pscore (Table A8)
if `i' == 3 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn hispanic female black white sp_biling pformany"
	local outcomes			`outcomes_set'
	local restriction		"in_K1"
	local suffix			"an_fr_K1_cntrl_lin_psc"
	local demographic_list	""
}

*This is the analysis that drops the female indicator from the set of control
*variables (Table A6)
if `i' == 4 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn hispanic black white sp_biling pformany"
	local pscore 			"pformany"
	local outcomes			`outcomes_set'
	local restriction		"in_K1"
	local suffix			"an_fr_K1_no_fem"
	local demographic_list	""
}
*This is the analysis that excludes the 1997 cohort (Table A7)
if `i' == 5 {
	local endog				att
	local instrument 		soffany
	local controls			"i.yr_asgn hispanic female black white sp_biling pformany"
	local pscore 			"pformany"
	local outcomes			`outcomes_set'
	local restriction		"in_K1 & yr_asgn != 1997"
	local suffix			"an_fr_K1_no_97"
	local demographic_list	""
}
*This is the analysis that uses the random number as instrument (Table A9)
if `i' == 6 {
	local endog				att
	local instrument 		random
	local controls			"i.yr_asgn hispanic female black white sp_biling pformany"
	local pscore			"pformany"
	local outcomes			`outcomes_set'
	local restriction		"in_K1"
	local suffix			"an_fr_K1_rand"
	local demographic_list	""
}
if `i'==7 {
	local outcomes_set2 any_6m_asgn any2yr_nsc_6m_asgn any4yr_nsc_6m_asgn instate_nsc_6m_asgn ///
					public_nsc_6m_asgn private_nsc_6m_asgn everany_nsc everany2yr_nsc ///
					everany4yr_nsc everinstate_nsc everpublic_nsc everprivate_nsc ///
					num_sem evergrad_nsc evergrad_nsc_4yr grad_ontime_asgn
*This analysis adds the p-score interacted with the endogenous as another endogenous variable
*and the p-score interacted with the instrument as another instrument
	local endog				"att d_psc_int"
	local instrument 		"soffany z_psc_int"
	local controls			"i.yr_asgn hispanic female black white sp_biling"
	local pscore			"pformany"
	local outcomes			`outcomes_set2'
	local restriction		"in_K1 & !inlist(pformany,0,1)"
	local suffix			"an_fr_K1_cntrl_nondegp_nonsingcells_pscint"
	local demographic_list	""
}

if `i' == 3 {
	preserve
	get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	outcomes(`outcomes') other_controls(`controls') restriction(`restriction') ///
	outfile("$results/tables/nsc_`suffix'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
	restore
}
else {
	preserve
	get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
	outcomes(`outcomes') other_controls(`controls') restriction(`restriction') pscore(`pscore') ///
	outfile("$results/tables/nsc_`suffix'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
	restore

}

****************************SUBGROUP ANALYSIS**********************************
* This command produces the sub-group estimates for the NSC outcomes
foreach demo of local demographic_list {
	if "`demo'" == "hisp" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & hispanic == 1"
	}
	if "`demo'" == "asian" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & asian == 1"
	}
	if "`demo'" == "black" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & black == 1"
	}
	if "`demo'" == "fem" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 1"
	}
	if "`demo'" == "male" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & female == 0"
	}
	if "`demo'" == "adv" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & (white == 1 | asian == 1)"
	}
	if "`demo'" == "urm" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & white != 1 & asian != 1"
	}
	if "`demo'" == "white" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & white == 1"
	}
	if "`demo'" == "frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & frpl == 1"
	}
	if "`demo'" == "not_frpl" {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & not_frpl == 1"
	}

	if inlist("`demo'","inc1" , "inc2","inc3","inc4") {
		local suffix_new	"`suffix'_set_`demo'"
		local restriction_new 	"`restriction' & `demo' == 1"
	}
	local controls "i.yr_asgn"

	* Only need to run this on the main Table 3 specification
	if `i' == 1 {
		preserve
		get_estimates, endogvar(`endog') instruments(`instrument') idvar(studentno) ///
		outcomes(`outcomes') other_controls(`controls') restriction(`restriction_new') pscore(`pscore') ///
		outfile("$results/tables/nsc_`suffix_new'") 2sls(1) rf(1) fs(1) non_offered_mean(1)
		restore
	}

}

}
